<?php

namespace App\Http\Controllers\service;

use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class FollowersController extends Controller
{
    /**
     * 关注
     */
    public function store(User $user)
    {
        if (Auth::user()->id === $user->id)
        {
            return redirect('/');
        }

        if (!Auth::user()->isFollowing($user->id))
        {
            Auth::user()->follow($user->id);
        }
        return redirect()->route('users.show',$user->id);
    }

    /**
     * 取消关注
     */
    public function destroy(User $user)
    {
        if (Auth::user()->id === $user->id)
        {
            return redirect('/');
        }
        if (!Auth::user()->isFollowing($user->id))
        {
            Auth::user()->unfollow($user->id);
        }

        return redirect()->route('users.show',$user->id);
    }
}
